import msg from "../../../utils/msg";
import { GetGradeActivity } from "../../../utils/request/api";
import User from "../../../utils/user";
import { listItem } from "../activity";

// pages/activity/activityHistory/activityHistory.ts
Page({

  /**
   * 页面的初始数据
   */
  data: {
    user:new User,
    show_list:[] as listItem[],
    origin_list:[] as listItem[],
  },
  //删除
  delete:async function(e:any){
      let id = e.currentTarget.dataset.id;
     await msg.confirm('是否删除编号为'+id+'的活动','点击确认即可删除');
     
  },
  add(){
      wx.navigateTo({
          url:'/pages/addActivity/addActivity',
      })
  },
  onClickLeft() {
    wx.navigateBack();
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    let self = this;
    GetGradeActivity({openid:this.data.user.openid})
    .then((res:any)=>{
        self.setData({
            origin_list:this.getListTime(res.data.data),
            show_list:this.getListTime(JSON.parse(JSON.stringify(res.data.data))),
        });
    })
  },
  //整理显示的时间的格式
  getListTime(list: listItem[]) {
    for (let i = 0; i < list.length; i++) {
      let item = list[i];
      list[i].time = this._getDateParts(item.start_time) + ' ' + this._getTime(item.start_time) + '-' + this._getTime(item.end_time);
    }
    let temp:listItem[] = [];
    for(let i = 0; i < list.length; i++){
        temp[i]=list[list.length-1-i];
    }
    return temp;
  },
  formatDate(dateString: string): string {
    const parts = dateString.split(" ");
    const year = parts[0].substring(0, 4);
    const month = parts[0].substring(6, 7);
    const day = parts[0].substring(9, 10);
    const hours = parts[1].substring(0, 2);
    const minutes = parts[1].substring(4, 5);
    const seconds = parts[1].substring(7, 8);
    //console.log(`${year}/${month}/${day} ${hours}:${minutes}:${seconds}`)
    return `${year}/${month}/${day} ${hours}:${minutes}:${seconds}`;
  },
  
  //获取日期的时间
  _getTime(date: string): string {
    const parsedDate = new Date(date);
    const hours = parsedDate.getHours();
    const minutes = parsedDate.getMinutes();
    return `${hours}:${minutes < 10 ? '0' + minutes : minutes}`;
  },
  //获取日期的年月日
  _getDateParts(dateString: string): string {
    const parts = dateString.split(" ");
    return parts[0];
  },
  onClickList(e:any){
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
        url:"../activitydetail/activitydetail?id="+id,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})